angular.module('moneyDetail', []).controller('moneyDetailCtrl', ['$scope', 'httpService', '$localStorage', '$rootScope', 'alertmsg', '$location', 'confirmservice', '$state', '$timeout','$stateParams' ,function($scope, httpService, $localStorage, $rootScope, alertmsg, $location, confirmservice, $state, $timeout,$stateParams) {
	$scope.start=$stateParams.start;
	$scope.end=$stateParams.end;
	$scope.type=null;
	$scope.mon=null;
	$scope.kind=null;
	$scope.timeItem=[];
	$scope.timeObj={
		id:null,
		name:null
	}
	$scope.pagenum = 1;
	$scope.limit=10;
	if ($location.search() && $location.search().page) {
        pagenum = $location.search().page;
    }
	$scope.updateUrlPage = function(page) {
        moneyDetailList(page);
    };
	//数据处理
	function detailChange(){
    		switch($scope.start) {
			case "1":
				$scope.mon="最近一个月";
				break;
			case "3":
				$scope.mon="最近三个月";
				break
			case "6":
				$scope.mon="最近半年";
				break
			case "12":
				$scope.mon="最近一年";
				break
		}
    		if($stateParams.type=='income'){
    			$scope.type="收入";
    			$scope.detail={
				type:$stateParams.type,
	    			start:$scope.start,
	    			end:$scope.end,
	    			page: $scope.pagenum,
	    			limit:$scope.limit
			} 
    		}else if($stateParams.type=='expense'){
  			$scope.type="支出";
  			$scope.detail={
				type:$stateParams.type,
	    			start:$scope.start,
	    			end:$scope.end,
	    			page: $scope.pagenum,
	    			limit:$scope.limit
			} 
    		}else if($stateParams.type==2){
    			$scope.kind="encashment";
    			$scope.type="提现";
    			$scope.detail={
				kind:$scope.kind,
	    			start:$scope.start,
	    			end:$scope.end,
	    			page: $scope.pagenum,
	    			limit:$scope.limit
			} 
    		}else if($stateParams.type==3){
    			$scope.kind="recharge";
    			$scope.type="充值";
    			$scope.detail={
				kind:$scope.kind,
	    			start:$scope.start,
	    			end:$scope.end,
	    			page: $scope.pagenum,
	    			limit:$scope.limit
			} 
    		}
    		moneyDetailList();
    }
	detailChange();
	//获取明细列表
	function moneyDetailList(page){
		console.log($scope.detail);
		 if (page) {
            $scope.pagenum = page;
            $scope.detail.page=$scope.pagenum;
        }
		httpService.query('web/payments/payments_detail',$scope.detail).then(function(res) {
            if (res.code == 200) {
            		console.log(res);
            		$scope.count=res.count/100;
            		$scope.moneyDetailList=res.payments;
            		$scope.page = res.page;
            		moneyDetailListChange();
            } else {
                alertmsg(res.msg);
            }
        });
	}
	//返回的数据处理
	function moneyDetailListChange(){
		//trade_type     ["alipay", "local", "alipay_pc_direct", "encashment", "finace_pay", "wx_pub", "alipay_wap", "profit", "wx_wap", "wx", "wechat"]
		//a开头支付宝   w开头微信   local站内交易  encashment提现  profit昨日收益     finace_pay品牌商支付
		for(var i in $scope.moneyDetailList) {
			if($scope.moneyDetailList[i].trade_type=='income'){
				$scope.moneyDetailList[i].trade_type="收入"
			}else{
				$scope.moneyDetailList[i].trade_type="支出"
			}
			var payString=$scope.moneyDetailList[i].pay_type.substr(0,1);
			switch(payString) {
				case "a":
					$scope.moneyDetailList[i].pay_type="支付宝"
					break;
				case "w":
					$scope.moneyDetailList[i].pay_type="微信"
					break
				case "l":
					$scope.moneyDetailList[i].pay_type="订单费用"
					break
				case "e":
					$scope.moneyDetailList[i].pay_type="提现"
					break
				case "p":
					$scope.moneyDetailList[i].pay_type="昨日收益"
					break
				case "f":
					$scope.moneyDetailList[i].pay_type="品牌商财务支付"
					break
			}
		}
	}
	//下拉框数据
	function selectDetail(){
		var date=new Date;
		var month=date.getMonth()+1;
		for(i=month,j=1;i>0,j<=month;i--,j++){
			$scope.timeObj.id=j;
			$scope.timeObj.name=i+"月";
			$scope.timeItem.push(angular.copy($scope.timeObj));
		}
	}
	selectDetail();
	//点击下拉框
	$scope.selectChange=function(name){
		$scope.detail.start=$scope.start;
		$scope.detail.end=$scope.start;
		$scope.mon=name;
		moneyDetailList();
	}
}]);
